﻿using mars_client.csharp.ao.impl;
using mars_client.csharp.AO;
using mars_client.csharp.cache;
using mars_client.csharp.enums;
using mars_client.csharp.model;
using mars_client.csharp.model.vo;
using mars_client.csharp.util;
using mars_client.csharp.vo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace mars_client.csharp.controller
{
    class Controller
    {
        private Controller(){}
        private readonly static Controller instance = new Controller();
        internal static Controller Instance
        {
            get
            {
                return instance;
            }
        }
        private Dictionary<string, Object> dic = new Dictionary<string, object>();
        public void router()
        {
            Task.Run(() =>
            {
                MessageCache mc = MessageCache.Instance;
                while (true)
                {
                    MessageCO co = mc.getLastestMessage();
                    if (co != null)
                    {
                        switch (co.type)
                        {
                            case (int)RequestEnum.request.Login:
                                {
                                    //MessageBox.Show("调用MainWindowAO");
                                    Task.Run(() =>
                                    {
                                        UserVO userVO = null;
                                        if (co.content != null)
                                        {
                                            userVO = TransformUtil.J2O<UserVO>(co.content);
                                        }
                                        MainWindowAOImpl.Instance.login(userVO);
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.GetFriends:
                                {
                                   // MessageBox.Show("调用MyFriendsAO");
                                    Task.Run(() =>
                                    {
                                        if(co.content != null)
                                        {
                                            FriendsVO friendsVO = TransformUtil.J2O<FriendsVO>(co.content);
                                            //List<UserVO> userVOs = new List<UserVO>();
                                            // co.content为JArray对象
                                            //foreach (JObject jo in (JArray)co.content)
                                            //{
                                            //    userVOs.Add(TransformUtil.J2O<UserVO>(jo));
                                            //}
                                            MyFriendsAOImpl.Instance.setMineFriends(friendsVO.userVOs);
                                        }
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.User:
                                {
                                    //MessageBox.Show("调用UserInfoAO");
                                    Task.Run(() =>
                                    {
                                        if(co.content != null)
                                        {
                                            UserVO userVO = TransformUtil.J2O<UserVO>(co.content);
                                            UserInfoImpl.Instance.showUserInfo(userVO);
                                        }
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.SingleChatContent:
                                {
                                    //MessageBox.Show("调用ChatAO");
                                    Task.Run(() =>
                                    {
                                        SingleChatVO singleChatVO = TransformUtil.J2O<SingleChatVO>(co.content);
                                        ChatAOImpl.Instance.setSingleChatContent(singleChatVO);
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.FindUser:
                                {
                                    //MessageBox.Show("调用MyFriendsAO FindUser");
                                    Task.Run(() =>
                                    {
                                        if (co.content != null)
                                        {
                                            //MessageBox.Show("执行转换");
                                            FriendsVO friendsVO = TransformUtil.J2O<FriendsVO>(co.content);
                                            MyFriendsAOImpl.Instance.setFoundedFriends(friendsVO.userVOs);
                                        }else
                                        {
                                            //MessageBox.Show("co.content为空");
                                        }
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.AddFriendNotify:
                                {
                                    //MessageBox.Show("调用MyFriendsAO");
                                    Task.Run(() =>
                                    {
                                        if (co.content != null)
                                        {
                                            MyFriendsAOImpl.Instance.notify(TransformUtil.J2O<FriendNotifyVO>(co.content));
                                        }
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.DeleteFriend:
                                {
                                    //MessageBox.Show("调用MyFriendAO deleteFriend方法");
                                    Task.Run(() =>
                                    {
                                        if(co.content != null)
                                        {
                                            MyFriendsAOImpl.Instance.deleteFriend(TransformUtil.J2O<UserVO>(co.content));
                                        }
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.CreateGroup:
                                {
                                    //MessageBox.Show("调用MyFriendAO 创建成功方法");
                                    Task.Run(() =>
                                    {
                                        if(co.content != null)
                                        {
                                            ChatAOImpl.Instance.createGroup(TransformUtil.J2O<GroupVO>(co.content));
                                        }
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.GroupChatContent:
                                {
                                    //MessageBox.Show("调用MyFriendAO 创建成功方法");
                                    Task.Run(() =>
                                    {
                                        if (co.content != null)
                                        {
                                            ChatAOImpl.Instance.setGroupContent(TransformUtil.J2O<GroupChatVO>(co.content));
                                        }
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.GetGroupUsers:
                                {
                                    Task.Run(() =>
                                    {
                                        if(co.content != null)
                                        {
                                            ChatAOImpl.Instance.showGroupUsers(TransformUtil.J2O<GroupVO>(co.content));
                                        }
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.GetFriendsForGroup:
                                {
                                    Task.Run(() =>
                                    {
                                        if (co.content != null)
                                        {
                                            ChatAOImpl.Instance.showFriendsForGroup(TransformUtil.J2O<FriendsVO>(co.content));
                                        }
                                    });
                                    break;
                                }
                            case (int)RequestEnum.request.GetGroupList:
                                {
                                    Task.Run(() =>
                                    {
                                        if(co.content != null)
                                        {
                                            ChatAOImpl.Instance.showGroups(TransformUtil.J2O<GroupListVO>(co.content));
                                        }
                                    });
                                    break;
                                }
                            default:
                                {
                                    MessageBox.Show("controller没写，type为"+co.type.ToString());
                                    break;
                                }
                        }
                    }
                    Thread.Sleep(50);
                }
            });
            
        }
    }
}
